package com.yunsc.order.sevice.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.utils.PageResult;
import com.yunsc.order.dao.YunscOrderDeliveryDao;
import com.yunsc.order.dto.QueryDeliveryDTO;
import com.yunsc.order.entity.YunscOrderDeliveryEntity;
import com.yunsc.order.sevice.YunscOrderDeliveryService;
import com.yunsc.order.sevice.YunscOrderGoodsService;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.order.vo.OrderDeliveryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service("yunscOrderDeliveryService")
public class YunscOrderDeliveryServiceImpl extends ServiceImpl<YunscOrderDeliveryDao, YunscOrderDeliveryEntity> implements YunscOrderDeliveryService {

    @Resource
    private YunscOrderGoodsService orderGoodsService;
    @Resource
    private YunscOrderService orderService;

    @Override
    public List<OrderDeliveryVO> queryDeliveryByOrderId(Integer orderId) {
        List<OrderDeliveryVO> orderDeliveryVO = this.baseMapper.selectOrderDeliveryByOrderId(orderId);
        return orderDeliveryVO;
    }

    @Override
    public PageResult<OrderDeliveryVO> orderDeliveryPageList(QueryDeliveryDTO param, Integer currentStoreId) {
        Page<OrderDeliveryVO> page =  this.baseMapper.orderDeliveryPageList(new Page<>(param.getPage(), param.getPageSize()), param, currentStoreId);
        page.getRecords().forEach(item -> {
            item.getGoods().forEach(goods -> {
                goods.setGoods(orderGoodsService.queryOrderGoodsById(goods.getOrderGoodsId()));
            });
            item.setOrderData(orderService.queryOrderDetailByOrderId(item.getOrderId()));
        });
        return PageResult.formatPageResult(page.getRecords(), param.getPage(), param.getPageSize(), page.getTotal());
    }
}